package cn.tedu.ivos.dict.service.impl;

import cn.tedu.ivos.dict.mapper.DictMapper;
import cn.tedu.ivos.dict.pojo.dto.DictQuery;
import cn.tedu.ivos.dict.pojo.dto.DictSaveParam;
import cn.tedu.ivos.dict.pojo.entity.Dict;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dict.service.DictService;
import cn.tedu.ivos.dictoption.mapper.DictOptionMapper;
import cn.tedu.ivos.dictoption.service.impl.DictOptionServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private DictOptionMapper dictOptionMapper;
    /**
     * 查询操作
     * @param dictQuery
     * @return
     */
    @Override
    public  List<DictVO> select(DictQuery dictQuery) {
        log.debug("{}",dictQuery);
       List<DictVO> dictVO = dictMapper.select(dictQuery);
        return dictVO;
    }

    /**
     * 新增操作
     * @param dictSaveParam
     */
    @Override
    public void save(DictSaveParam dictSaveParam) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictSaveParam,dict);
        if(dict.getId()==null){
            dict.setCreateTime(new Date());
            dictMapper.insert(dict);
        }else {
            dict.setUpdateTime(new Date());
            dictMapper.update(dict);
        }
    }

    @Override
    public void delete(DictSaveParam dictSaveParam) {
        log.debug("{}",dictSaveParam);
        dictMapper.delete(dictSaveParam);
        dictOptionMapper.deleteByDictId(dictSaveParam.getId());
    }
}
